package com.assessment.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.assessment.config.DataListener;
import com.assessment.entity.Adep;
import com.assessment.entity.Apeo;
import com.assessment.entity.Auser;
import com.assessment.entity.Column;
import com.assessment.service.AdepService;
import com.assessment.service.ApeoService;
import com.assessment.service.AroleService;
import com.assessment.service.AuserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.assessment.utils.Constant.*;
import static com.assessment.utils.ReturnMap.failedMap;
import static com.assessment.utils.ReturnMap.successMap;
import static com.baomidou.mybatisplus.core.toolkit.ObjectUtils.isEmpty;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@CrossOrigin("*")
@RestController
@Api(tags={"登录"})
@RequestMapping("/admin")
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    private final AuserService service;
    private final ApeoService apeoService;

    public LoginController(AuserService service,ApeoService apeoService) {
        this.service = service;
        this.apeoService = apeoService;
    }

    @PostMapping("/login")
    public Map<String,Object>  login(@RequestBody Auser auser){
        try {
            QueryWrapper<Auser> auserQueryWrapper = new QueryWrapper<>();
            auserQueryWrapper.eq("account", auser.getAccount());
            Auser one = service.getOne(auserQueryWrapper);
            if (one == null) return failedMap(LOGIN_FAILED);
            if (!(one.getPwd().equals(auser.getPwd()))){
                return failedMap(LOGIN_FAILED);
            }
            QueryWrapper<Apeo> apeoQueryWrapper = new QueryWrapper<>();
            apeoQueryWrapper.eq("pid", auser.getAccount());
            Apeo one1 = apeoService.getOne(apeoQueryWrapper);
            one.setPwd(one1 != null ? one1.getDid() : "");

            return successMap("登录成功!",one);

        }catch (Exception e){
            e.printStackTrace();
            return failedMap(LOGIN_FAILED);
        }
    }

}

